<template>
  <section class="component warehouse-management-approal">
    <data-form :model="dataForm" @onSearch="refreshData" :page="pageService" @handleResetForm="handleResetForm">
      <template slot="inputs">
        <el-form-item label="订单号" prop="caseNumber">
          <el-input v-model="dataForm.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="dataForm.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="dataForm.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="dataForm.personalName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="dataForm.idCard"></el-input>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="dataForm.loanDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="dataForm.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="dataForm.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNum">
          <el-input v-model="dataForm.plateNum"></el-input>
        </el-form-item>
        <el-form-item label="车辆状态" prop="warehouseCarStatus">
          <el-select v-model="dataForm.warehouseCarStatus" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('WarehouseCarStatus')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker type="datetimerange" :editable="false" v-model="dataForm.applyTime" placeholder="选择日期时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="申请人" prop="applyRealName">
          <el-input v-model="dataForm.applyRealName"></el-input>
        </el-form-item>
        <el-form-item label="车库" prop="garageName">
          <el-input v-model="dataForm.garageName"></el-input>
        </el-form-item>
        <el-form-item label="库管" prop="userName">
          <el-input v-model="dataForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="dataForm.partnerName"></el-input>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="dataForm.bankName"></el-input>
        </el-form-item>
        <el-form-item label="催收总金额" prop="overdueAmount">
          <number-range v-model="dataForm.overdueAmount" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="dataForm.businessAddress"></el-input>
        </el-form-item>

      </template>
    </data-form>
    <data-box :data="dataSet" @onPageChange="refreshData" :page="pageService">
      <template slot="columns">
        <el-table-column label="操作" :min-width="$helper.getColumnWidth(5)">
          <template slot-scope="scope">
            <el-button @click="onClickGarage(scope.row)" type="text" v-if="scope.row.userId === userId && scope.row.warehouseCarStatus === 'IN_WAREHOUSE_WAIT_APPROVED'" v-auth="1540">入库审核</el-button>
            <el-button @click="onClickGarage(scope.row)" type="text" v-if="scope.row.userId === userId && scope.row.warehouseCarStatus === 'OUT_WAREHOUSE_WAIT_APPROVED'" v-auth="1541">出库审核</el-button>
            <el-button @click="onClickGarage(scope.row)" type="text" v-if="scope.row.userId === userId && scope.row.warehouseCarStatus === 'TEMPORARY_OUT_WAIT_IN'" v-auth="1542">临时入库登记</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="plateNum" label="车牌号" :min-width="$helper.getColumnWidth(3)"></el-table-column>
        <el-table-column prop="warehouseCarStatus" label="车辆状态" :formatter="(row) => $filter.dictConvert(row.warehouseCarStatus,'WarehouseCarStatus')" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyRealName" label="申请人" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyTime" label="申请时间" :min-width="$helper.getColumnWidth(5)" :formatter="(row) => $filter.dateTimeFormat(row.applyTime)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="garageName" label="车库" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="userName" label="库管" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
      </template>
    </data-box>

    <el-dialog :title="titleName" :center="true" :visible.sync="dailog.warehousingShow" width="650px" @open="$nextTick(() => $refs['warehousing-audit'].imageFileList())">
      <warehousing-audit :rowData="rowData" @close="dailog.warehousingShow = false;refreshData()" ref="warehousing-audit" />
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'
import { Auth, Layout, Dependencies } from '~/core/decorator';
import DataBox from '~/components/common/data-box.vue'
import DataForm from '~/components/common/data-form.vue'
import { PageService } from '~/utils/page.service'
import NumberRange from '~/components/common/number-range.vue';
import { ApplyType, CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import { State, namespace } from "vuex-class";
import { CarManageService } from "~/services/domain-service/car-manage.service";
import WarehousingAudit from '~/components/car-management/warehousing-audit.vue'
const CollectionExecutionModule = namespace("collection-execution");

@Auth(1534)
@Layout('workspace')
@Component({
  components: {
    DataBox,
    DataForm,
    NumberRange,
    WarehousingAudit
  }
})

export default class WarehouseManagementApproal extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @State private principalList: any;
  @Dependencies(CarManageService) private carManageService: CarManageService
  @State private userData

  private dataSet: any = []
  private dataForm: any = {
    batchNumber: "",
    mobileNo: "",
    personalName: "",
    overdueAmount: { min: "", max: "" },
    caseNumber: '',
    principalId: '',
    partnerName: '',
    bankName: '',
    businessAddress: '',
    boardAddress: '',
    plateNum: '',
    idCard: '',
    loanDate: [],
    applyTime: [],
    applyRealName: '',
    warehouseCarStatus: '',
    garageName: '',
    userName: ''
  };
  private dailog: any = {
    warehousingShow: false
  }
  private rowData: any = {}

  private get titleName() {
    return this.rowData.warehouseCarStatus === 'IN_WAREHOUSE_WAIT_APPROVED' ? '入库审核' : this.rowData.warehouseCarStatus === 'OUT_WAREHOUSE_WAIT_APPROVED' ? '出库审核' : '临时入库登记'
  }

  private get userId() {
    return this.userData.id
  }

  private created() {
    this.refreshData()
  }

  private params() {
    const params = { ...this.dataForm }
    params.overdueAmountMax = this.dataForm.overdueAmount.max
    params.overdueAmountMin = this.dataForm.overdueAmount.min
    params.loanDateMin = this.$filter.dateRanageFormat(this.dataForm.loanDate).start
    params.loanDateMax = this.$filter.dateRanageFormat(this.dataForm.loanDate).end
    params.applyTimeMin = this.dataForm.applyTime.length ? this.$filter.dateTimeFormat(this.dataForm.applyTime[0]) : ''
    params.applyTimeMax = this.dataForm.applyTime.length ? this.$filter.dateTimeFormat(this.dataForm.applyTime[1]) : ''
    delete params.overdueAmount
    delete params.loanDate
    delete params.applyTime
    return params
  }

  private refreshData() {
    this.carManageService.getAllWarehouseApproval(this.params(), this.pageService).subscribe(data => {
      this.dataSet = data
    })
  }

  private onClickGarage(row) {
    this.rowData = row;
    this.dailog.warehousingShow = true
  }
  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Car_warehouse_approval,
      hiddenCaseList: true,
      hiddenAction: true
    })
  }

  private handleResetForm() {
    this.dataForm.overdueAmount = { min: "", max: "" }
  }
}
</script>